//
//  UITextViewController.swift
//  DemoUI
//
//  Created by 白瑾浩 on 2022/2/22.
//

import UIKit

class UITextViewController: UIViewController, UITextViewDelegate {
    //基础使用
    lazy var textView: UITextView = {
        let textView = UITextView.init()
        textView.frame = CGRect(x: 20, y: 100, width: self.view.bounds.width-40, height: 200)
        textView.clipsToBounds = true //剪辑边界
        textView.textColor = UIColor.orange //字体颜色
        textView.layer.cornerRadius = 10 //圆角
        textView.layer.borderWidth = 1.0 //边框
        textView.layer.borderColor = UIColor.gray.cgColor //边框颜色
        textView.font = UIFont.systemFont(ofSize: 17) //字体大小
        textView.keyboardType = UIKeyboardType.default // 键盘类型
        textView.returnKeyType = UIReturnKeyType.done // 键盘右下键 按钮类型
        textView.tag = 1000
        textView.layoutManager.allowsNonContiguousLayout = false //解决内容显示不全的问题
        return textView
    }()
    //自适应高度
    lazy var textViewAutoHeight: UITextView = {
        let textView = UITextView.init()
        textView.frame = CGRect(x: 20, y: 400, width: self.view.bounds.width-40, height: 40)
        textView.clipsToBounds = true //剪辑边界
        textView.textColor = UIColor.orange //字体颜色
        textView.layer.cornerRadius = 10 //圆角
        textView.layer.borderWidth = 1.0 //边框
        textView.layer.borderColor = UIColor.gray.cgColor //边框颜色
        textView.font = UIFont.systemFont(ofSize: 17) //字体大小
        textView.keyboardType = UIKeyboardType.default // 键盘类型
        textView.returnKeyType = UIReturnKeyType.done // 键盘右下键 按钮类型
        textView.tag = 2000
        textView.layoutManager.allowsNonContiguousLayout = false
        return textView
    }()
    
    
    //自定义提示语placeholder
    lazy var textViewplaceholder: UITextView = {
        let textView = UITextView.init()
        textView.frame = CGRect(x: 20, y: 600, width: self.view.bounds.width-40, height: 40)
        textView.clipsToBounds = true //剪辑边界
        textView.textColor = UIColor.orange //字体颜色
        textView.layer.cornerRadius = 10 //圆角
        textView.layer.borderWidth = 1.0 //边框
        textView.layer.borderColor = UIColor.gray.cgColor //边框颜色
        textView.font = UIFont.systemFont(ofSize: 17) //字体大小
        textView.keyboardType = UIKeyboardType.default // 键盘类型
        textView.returnKeyType = UIReturnKeyType.done // 键盘右下键 按钮类型
        textView.tag = 3000
        textView.layoutManager.allowsNonContiguousLayout = false
        return textView
    }()
    
    var placeholderLabel:UILabel?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        self.title = "UITextView"
        
        //基础使用
        self.view.addSubview( textView )
        
        //自适应高度
        self.view.addSubview( textViewAutoHeight )
        
        textViewAutoHeight.delegate = self
        
        
        //自定义placeholder
        self.view.addSubview( textViewplaceholder )
        textViewplaceholder.delegate = self
        // 制作placeholder
        self.placeholderLabel = UILabel.init() // placeholderLabel是全局属性
        self.placeholderLabel?.frame = CGRect(x: 5, y: 5, width: 100, height: 20)
        self.placeholderLabel?.font = UIFont.systemFont(ofSize: 13)
        self.placeholderLabel?.text = "我是placeholder"
        textViewplaceholder.addSubview(self.placeholderLabel!)
        self.placeholderLabel?.textColor = UIColor.init(red: 72/256, green: 82/256, blue: 93/256, alpha: 1)
        
        
        
    }
    //计算高度
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        
        if textView.tag == 2000 {  //自适应高度的UITextView
            //textView.bounces 阻尼
            if (textView.contentSize.height != textView.bounds.size.height) && textView.bounds.size.height < 100 {
                textView.frame.size.height = textView.contentSize.height
                UIView.animate(withDuration: 0.3) {
                    self.view.layoutIfNeeded()
                }
            }
        }
        return true
    }
    
    
    //MARK: UITextViewDelegate
    func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
        if textView.tag == 3000 {
            print("textViewShouldBeginEditing")
            self.placeholderLabel?.isHidden = true // 隐藏
        }
        return true
    }
    func textViewDidBeginEditing(_ textView: UITextView) {
        
    }
    func textViewShouldEndEditing(_ textView: UITextView) -> Bool {
        
        print("textViewShouldEndEditing")
        return true
    }
    func textViewDidEndEditing(_ textView: UITextView) {
        if textView.tag == 3000 {
            if textView.text.isEmpty {
                self.placeholderLabel?.isHidden = false  // 显示
            }
            else{
                self.placeholderLabel?.isHidden = true  // 隐藏
            }
        }
        print("textViewDidEndEditing")
    }
    func textViewDidChange(_ textView: UITextView) {
        print("textViewDidChange")
    }
    func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
        
        print("shouldChangeTextInRange")
        if text == "\n"{ // 输入换行符时收起键盘
            textView.resignFirstResponder() // 收起键盘
        }
        return true
    }
    
    //隐藏软键盘
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?){
        textView.resignFirstResponder()
        textViewAutoHeight.resignFirstResponder()
        textViewplaceholder.resignFirstResponder()
    }
}
